package com.example.aspectjdemo;

import android.util.Log;
import android.view.View;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.util.Calendar;

/**
 * author : king-nina
 * e-mail : 13450414852@163.com
 * date   : 2021/10/2714:30
 * desc   :
 */
@Aspect
public class FastClickAspect {

    static int TIME_TAG = R.id.click_time;
    public static final int FAST_CLICK_TIME = 1000;


    //execution方法里面要填写你自己的完整的包名路径：xxx.xxx.xxx.SingleClick
    @Pointcut("execution(@com.example.aspectjdemo.FastClick * *(..))")
    public void methodAnnotated() {
    }

    @Around("methodAnnotated()")
    public void aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        View view = null;
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof View) {
                view = (View) arg;
            }
        }

        if (view != null) {
            Object tag = view.getTag(TIME_TAG);
            long lastClickTime = ((tag != null) ? (long) tag : 0);
            long currentTime = Calendar.getInstance().getTimeInMillis();
            if (currentTime - lastClickTime > FAST_CLICK_TIME) {
                view.setTag(TIME_TAG, currentTime);
                joinPoint.proceed();//执行原方法
            }
        }
    }
}
